package com.zeng.plugin.util;

import com.zeng.plugin.entity.TableEntity;
import freemarker.template.TemplateException;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.dom4j.DocumentException;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@NoArgsConstructor
@Data
public class CreatePojo {
    //    接收java文件存放的路径
    private String directory;

    // 项目资源文件的目录
    private String resourceDir;
    //文件包名
    private String fullPackage;
    //表名前缀
    private String perfix;

    public CreatePojo(String resourceDir, String fullPackage, String perfix) {
        this.perfix = perfix;

        this.fullPackage = fullPackage;

        this.directory = new File(resourceDir + "/../java" + "/" + fullPackage.replace(".", "/")).getAbsolutePath();

        this.resourceDir = resourceDir;

    }

    public void createPoJo(List<TableEntity> tableEntities,FreeMarkUtil freeMarkUtil) throws IOException, TemplateException, ClassNotFoundException, DocumentException {

        File file1 = new File(directory);
        if (!file1.exists()) {
            file1.mkdirs();
        }
        Map<String, Object> data = new HashMap<>();
        data.put("packageName", fullPackage);
        data.put("prefix", perfix);
        for (TableEntity tableEntity : tableEntities) {
            data.put("table", tableEntity);
            File file = new File(directory + "//" + tableEntity.getClassName() + ".java");

            if (file.exists()) {
                continue;
            }

            String fileName = new File(directory).getAbsolutePath() + "/" + tableEntity.getClassName() + ".java";
            freeMarkUtil.parse("template.ftl", fileName, data);
            System.out.println(fileName);
            System.out.println(tableEntity.getClassName() + "创建完成");
        }

    }

//    public static void main(String[] args) throws ClassNotFoundException, TemplateException, DocumentException, IOException {
//        CreatePojo createPojo = new CreatePojo();
//        String s = "com.zhku.zeng";
//        String s2 = "test-plugin-execute";
//        String tempDir = s.replace(".", "\\") +"\\"+ s2.replace("-", "");
////        createPojo.createPoJo(new File("").getAbsolutePath());
//
//
//
//    }
}
